package com.gzx.plugin.tjzy.modular.work.service.impl;

import java.io.FileOutputStream;
import java.util.List;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelLitigationFileWriter {
	// 催记Excel
	public static String createMExcel(List<LitigationFile> dataList, String dataName) throws Exception {
		Workbook workbook = new XSSFWorkbook();
		Sheet sheet = workbook.createSheet("诉讼文件");

		// 创建表头
		Row headerRow = sheet.createRow(0);
		headerRow.createCell(0).setCellValue("业务号");
		headerRow.createCell(1).setCellValue("文件名");
		headerRow.createCell(2).setCellValue("文件内容");

		// 填充数据
		for (int i = 0; i < dataList.size(); i++) {
			LitigationFile vo = dataList.get(i);
			Row row = sheet.createRow(i + 1);
			row.createCell(0).setCellValue(vo.getBusinessId());
			row.createCell(1).setCellValue(vo.getFileName());
			row.createCell(2).setCellValue(vo.getFileNote());
		}

		// 自动调整列宽
		for (int i = 0; i < 3; i++) {
			sheet.autoSizeColumn(i);
		}

		// 诉讼文件数据以 file- 开头 .xlsx或xls结尾
		String filePath = "file-" + dataName + ".xlsx";
		try (FileOutputStream fos = new FileOutputStream(filePath)) {
			workbook.write(fos);
		}
		workbook.close();
		return filePath;
	}

}
